package com.vms.healthcontrol;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import com.vms.NetLog;
import com.vms.Settings.ValueType;
import com.vms.control.weight.input.InputSettingsFactory;

public class ValueUnderControl {
	
	public static class ValueLimits {
		public float mLocalMinimum;
		public float mLocalMaximum;
		public float mPossibleMinimum;
		public float mPossibleMaximum;
	}

	protected String mShowName;
	protected String mDescription;
	protected String mDimension;

	protected ValueType mDbName;
	protected Date mModifyDate;
	protected float mValue;
	protected float mChangeDelta;
	
	protected ValueLimits limits = new ValueLimits();
	protected boolean mModifiedInSession = false;

	
	public boolean isModifiedInSession() {
		return mModifiedInSession;
	}
	
	public void setModifiedInSession (boolean isModified) {
		this.mModifiedInSession = isModified;
	}

	protected ValueUnderControl ( ) {
	}
	
	public void set ( float value,  Date changeDate ) {
		this.mValue = value;
		this.mModifyDate = changeDate;
	}
	
	public static ValueUnderControl create ( InputSettingsFactory settings ) {
		
		ValueUnderControl value = new ValueUnderControl();
		
		value.mDbName = settings.getType();
		value.mShowName = settings.getShowName();
		value.mDescription = settings.getDescription();
		value.mDimension = settings.getDimension();
		value.limits.mPossibleMinimum = settings.getPossibleMinimum();
		value.limits.mPossibleMaximum = settings.getPossibleMaximum();
		value.limits.mLocalMaximum = settings.getLocalMaximum();
		value.limits.mLocalMinimum = settings.getLocalMinimum();
		value.mChangeDelta = settings.getChangeDelta();
		value.mValue = settings.getValue();
		value.mModifyDate = new Date();
		
		return value;
		
	}

	public void increase() {
		mModifiedInSession = true;
		this.mValue += this.mChangeDelta;
	}
	
	public void decrease() {
		mModifiedInSession = true;
		this.mValue -= this.mChangeDelta;		
	}
	
	public String getShowName() {
		return mShowName;
	}
	
	public String getShowDescr() {
		return mDescription;
	}

	public String getShowDimension() {
		return mDimension;
	}

	public float getValue() {
		return mValue;
	}
	
	@Deprecated
	public String getValueString() {
		return String.format(Locale.UK,"%.2f", mValue);
	}


	public void setValue(float value) {
		this.mValue = value;
	}

	public Date getModify() {
		return mModifyDate;
	}

	public void setModify(Date modify) {
		this.mModifyDate = modify;
	}
	
	/**
	 *  dd.MM.yyyy
	 */
	public String getDateStr () {
		return new SimpleDateFormat("dd.MM.yyyy").format(mModifyDate);
	}
	
	/**
	 *  yyyyMMdd 
	 */
	public String getDbDate() {
		return getDateLongStrForDb(mModifyDate);
	}
	
	public static String getDateLongStrForDb(Date date) {
		return new SimpleDateFormat("yyyyMMdd").format(date);
	}

	public void setFromDatabase(String name, float value, Date date) {
		this.mValue = value;
		this.mModifyDate = date;
		this.mShowName = name;

		
		this.limits.mLocalMinimum = 0.0f;
		this.limits.mLocalMaximum = 0.0f;
		
	}

	public String getDbName() {
		return mDbName.mName;
	}
	
	public void dump() {
		NetLog.dd(this.toString());
	}
	
	@Override
	public String toString() {
		return getDbDate() + " " + getDbName() + " " + getValue();
	}

	
}
